<?php
namespace Home\Controller;

use Think\Controller;

/**
 * Class NoticeController
 * 公告中心控制器
 */
class NoticeController extends CommnController{
    /**
     * 公告列表
     */
    public function noticelist(){
        $this->roles = role(I('rid'));
        $this->rid=I('rid');
        $notice_db =M('notice','','KEFU');
        $where['a.status']=1;
        $where['a.type']=1;
        if(!empty(I('get.notice'))){
            $where['title']=array('like','%'.I('get.notice').'%');
        }
        $tol =$notice_db->alias('a')->join('apart b on a.apart_id=b.id')->where($where)->count();
        $row = '10';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field='b.name as apart_name,a.name,a.id,a.title,a.content,a.ctime,a.status';
        $notice=$notice_db->alias('a')->join('apart b on a.apart_id=b.id')->field($field)->order('a.ctime desc')->where($where)->LIMIT($page->listfirst, $page->listRows)->select();
        foreach ($notice as $k=>$v){
            if(!empty($v['ctime'])){
                $notice[$k]['ctime']=date('Y-m-d H:i',$v['ctime']);
            }
        }
        $this->assign(array(
            'notice' => $notice,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->notice_title=I('get.notice');
        $this->display();
    }

    /**
     * 发布公告
     */
    public function addnotice(){
        $title = I('title');
        $content = I('content');
        if (empty($title) && empty($content)) {
            $this->display();
            die;
        }
        $apartid = M('apart','','KEFU')->where(['name'=>['like','%客服部门%']])->getfield('id');

        $arr = array('apart_id' => $apartid, 'name' => $_SESSION['name'], 'title' => $title, 'content' => $content, 'ctime' => time(),'type'=>1);
        $add = M('notice', '', 'KEFU')->add($arr);
        //添加到操作日志
        $arr = array('aid' => $_SESSION['id'], 'ctime' => time(), 'content' => '发布公告-' . $add);
        $log = M('admin_log', '', 'KEFU')->add($arr);

        if ($add) {
            $this->ajax('1001', '添加成功');
        } else {
            $this->ajax('1002', '添加失败' . '1' . '=' . $_SESSION['name'] . $title . $content);
        }
    }

    /**
     * 公告详情
     */
    public function details($id){
        $page=I('page');
        $notice=M('notice','','KEFU')->where(['id'=>$id])->field('status',true)->find();
        $notice['part']=M('apart','','KEFU')->where(['id'=>$notice['apart_id']])->getfield('name');
        if(!empty($notice['ctime'])){
            $notice['ctime']=date('Y-m-d H:i',$notice['ctime']);
        }
        $this->notice=$notice;
        $this->page=$page;
        $this->display();
    }

}